package cn.redanmedia.rd.ly.api.test;

import cn.stylefeng.qz.core.util.Json;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @description 
 * @author qinj
 * @date 2018/9/27 23:15
 */
public class HttpUtils {


    public static String sendGet(String url) {
        return sendGet(url, null, null, HttpType.NORMAL);
    }

    public static String sendGet(String url, Map<String, String> header) {
        return sendGet(url, header, null, HttpType.NORMAL);
    }

    public static String sendGet(String url, Map<String, String> header, Map<String, Object> bodys) {
        return sendGet(url, header, bodys, HttpType.NORMAL);
    }

    public static String sendGet(String url, Map<String, String> header, Map<String, Object> bodys, HttpType httpType) {
        HttpHeaders requestHeaders = new HttpHeaders();
        if(header != null && !header.isEmpty()) {
            for (Map.Entry<String, String> me : header.entrySet())
                requestHeaders.add(me.getKey(), me.getValue());
        }
        ResponseEntity<String> response = null;
        if(httpType.equals(HttpType.JSON)) {
            requestHeaders.add("Content-Type", "application/json");
            String body = bodys != null ? Json.toJsonString(bodys) : "";
            HttpEntity<String> requestEntity = new HttpEntity<>(body, requestHeaders);
            RestTemplate template = new RestTemplate();
            response = template.exchange(url, HttpMethod.GET, requestEntity, String.class);
        } else if(httpType.equals(HttpType.NORMAL)) {
            MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
            if(bodys != null && !bodys.isEmpty()) {
                for (Map.Entry<String, Object> me : bodys.entrySet()) {
                    if(!url.contains("?"))
                        url = url + "?";
                    else
                        url += "&";
                    url += me.getKey() + "=" + me.getValue();
                }
//                    body.add(me.getKey(), me.getValue().toString());
            }
            HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(body, requestHeaders);
            RestTemplate template = new RestTemplate();
            try {
                response = template.exchange(url, HttpMethod.GET, requestEntity, String.class);
            } catch (HttpClientErrorException e) {
                return e.getResponseBodyAsString();
            }
        } else
            throw new RuntimeException("未知请求参数类型!");
        return response.getBody();
    }

    public static String sendPost(String url) {
        return sendPost(url, null, null, HttpType.NORMAL);
    }

    public static String sendPost(String url, Map<String, Object> header, Map<String, Object> bodys) {
        return sendPost(url, header, bodys, HttpType.NORMAL);
    }

    public static String sendPost(String url, Map<String, Object> header, Object bodys, HttpType httpType) {
        try {
            HttpHeaders requestHeaders = new HttpHeaders();
            if (header != null && !header.isEmpty()) {
                for (Map.Entry<String, Object> me : header.entrySet())
                    requestHeaders.add(me.getKey(), me.getValue().toString());
            }
            ResponseEntity<String> response = null;
            if (httpType.equals(HttpType.JSON)) {
                requestHeaders.add("Content-Type", "application/json; charset=UTF-8");
                String body = bodys != null ? Json.toJsonString(bodys) : "";
                HttpEntity<String> requestEntity = new HttpEntity<>(body, requestHeaders);
                RestTemplate template = new RestTemplate();

                response = template.exchange(url, HttpMethod.POST, requestEntity, String.class);
            } else if (httpType.equals(HttpType.NORMAL)) {
                MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
                if (bodys != null && !((Map<String, Object>) bodys).isEmpty()) {
                    for (Map.Entry<String, Object> me : ((Map<String, Object>) bodys).entrySet()) {
                        if (me.getValue() == null)
                            continue;
                        body.add(me.getKey(), me.getValue().toString());
                    }
                }
                HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(body, requestHeaders);
                RestTemplate template = new RestTemplate();
                response = template.exchange(url, HttpMethod.POST, requestEntity, String.class);
            } else
                throw new RuntimeException("未知请求参数类型!");

            return response.getBody();
        }catch(HttpClientErrorException e){
            return e.getResponseBodyAsString();
        }
    }

    public static String sendPut(String url) {
        return sendPut(url, null, null, HttpType.NORMAL);
    }

    public static String sendPut(String url, Map<String, String> header) {
        return sendPut(url, header, null, HttpType.NORMAL);
    }

    public static String sendPut(String url, Map<String, String> header, Map<String, Object> bodys, HttpType httpType) {
        HttpHeaders requestHeaders = new HttpHeaders();
        if(header != null && !header.isEmpty()) {
            for (Map.Entry<String, String> me : header.entrySet())
                requestHeaders.add(me.getKey(), me.getValue());
        }
        ResponseEntity<String> response = null;
        if(httpType.equals(HttpType.JSON)) {
            requestHeaders.add("Content-Type", "application/json");
            String body = bodys != null ?  Json.toJsonString(bodys) : "";
            HttpEntity<String> requestEntity = new HttpEntity<>(body, requestHeaders);
            RestTemplate template = new RestTemplate();
            response = template.exchange(url, HttpMethod.PUT, requestEntity, String.class);
        } else if(httpType.equals(HttpType.NORMAL)) {
            MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
            if(bodys != null && !bodys.isEmpty()) {
                for (Map.Entry<String, Object> me : bodys.entrySet())
                    body.add(me.getKey(), me.getValue().toString());
            }
            HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(body, requestHeaders);
            RestTemplate template = new RestTemplate();
            response = template.exchange(url, HttpMethod.PUT, requestEntity, String.class);
        } else
            throw new RuntimeException("未知请求参数类型!");
        return response.getBody();
    }

    public enum HttpType {
        JSON,
        NORMAL
    }
}
